// 数组方法总结：
// 1. toString() 方法：将数组转换为字符串。
// 2. toLocaleString() 方法：将数组转换为本地化的字符串。
// 3. join() 方法：将数组元素用指定字符连接成字符串。
// 4. pop() 方法：删除数组最后一个元素。
// 5. push() 方法：在数组末尾添加一个或多个元素。
// 6. shift() 方法：删除数组第一个元素。
// 7. unshift() 方法：在数组开头添加一个或多个元素。
// 8. concat() 方法：将两个或多个数组合并成一个数组。
// 9. reverse() 方法：将数组元素的顺序反转。
// 10. sort() 方法：对数组元素进行排序。
// 11. splice() 方法：在数组中新增或删除元素。
// 12. slice() 方法：从数组中提取一个新的数组。
// 13. indexOf() 方法：查找数组中第一个指定元素的索引。
// 14. lastIndexOf() 方法：查找数组中最后一个指定元素的索引。
// 15. find() 方法：查找数组中第一个满足条件的元素。
// 16. findIndex() 方法：查找数组中第一个满足条件的元素的索引。
// 17. filter() 方法：创建一个新数组，包含所有满足条件的元素。
// 18. fill() 方法：用指定值填充数组元素。
// 19. every() 方法：测试数组中所有元素是否都满足条件。
// 20. some() 方法：测试数组中是否有元素满足条件。
// 21. forEach() 方法：对数组中的每个元素执行一次指定操作。
// 22. map() 方法：创建一个新数组，对原数组的每个元素执行一次指定操作。
// 23. reduce() 方法：对数组中的元素执行指定操作，将结果汇总为单一值。
// 24. reduceRight() 方法：对数组中的元素执行指定操作，将结果汇总为单一值，从右到左。


// toString() method is used to convert an array to a string.

let arr = [1, 2, 3, 4, 5];
let str = arr.toString();
console.log(str); // Output: 1,2,3,4,5

// toLocaleString() method is used to convert an array to a string with locale specific formatting.

let arr1 = [1000, 2000, 3000, 4000, 5000];
let str1 = arr1.toLocaleString();
console.log(str1); // Output: 1,000,2,000,3,000,4,000,5,000

// join() method is used to join all elements of an array into a string.

let arr2 = [1, 2, 3, 4, 5];
let str2 = arr2.join("|");
console.log(str2); // Output: 1|2|3|4|5


console.log('**********************************')

// pop push shift unshift methods are used to manipulate an array.

let arr3 = [1, 2, 3, 4, 5];
arr3.pop();
console.log(arr3); // Output: [1, 2, 3, 4]

arr3.push(6, 7);
console.log(arr3); // Output: [1, 2, 3, 4, 6, 7]


arr3.shift();
console.log(arr3); // Output: [2, 3, 4, 6, 7]

arr3.unshift(1);
console.log(arr3); // Output: [1, 2, 3, 4, 6, 7]

console.log('**********************************')


// concat() method is used to merge two or more arrays.

let arr4 = [1, 2, 3];
let arr5 = [4, 5, 6];
let arr6 = arr4.concat(arr5);
console.log(arr6); // Output: [1, 2, 3, 4, 5, 6]


// reverse() method is used to reverse the order of elements in an array.

let arr7 = [1, 2, 3, 4, 5];
arr7.reverse();
console.log(arr7); // Output: [5, 4, 3, 2, 1]

// sort() method is used to sort the elements of an array in place.

let arr8 = [5, 2, 8, 3, 1];
arr8.sort((a, b) => a - b);
console.log(arr8); // Output: [1, 2, 3, 5, 8]

// splice() method is used to add or remove elements from an array. 改变原数组数组

let arr9 = [1, 2, 3, 4, 5];
arr9.splice(2, 0, 6, 7);
console.log(arr9); // Output: [1, 2, 6, 7, 3, 4, 5]

arr9.splice(2, 2);
console.log(arr9); // Output: [1, 2, 3, 4, 5]

// slice() method is used to extract a portion of an array. 不会改变原数组


let arr10 = [1, 2, 3, 4, 5];
let newArr = arr10.slice(2, 4);
console.log(newArr); // Output: [3, 4]


console.log('**********************************')

// indexOf() method is used to find the first occurrence of an element in an array. 返回元素的索引

let arr11 = [1, 2, 3, 4, 5];
let index = arr11.indexOf(3);
console.log(index); // Output: 2

// lastIndexOf() method is used to find the last occurrence of an element in an array. 返回元素的索引

let arr12 = [1, 2, 3, 4, 5, 3, 2];
let lastIndex = arr12.lastIndexOf(2);
console.log(lastIndex); // Output: 6

// find() method is used to find the first element that satisfies the provided testing function. 返回满足条件的第一个元素

let arr13 = [1, 2, 3, 10, 5];
let found = arr13.find(num => num > 3);
console.log(found); // Output: 10

// findIndex() method is used to find the index of the first element that satisfies the provided testing function. 返回满足条件的第一个元素的索引

let arr14 = [1, 2, 3, 10, 5];
let indexFound = arr14.findIndex(num => num > 3);
console.log(indexFound); // Output: 3

console.log('**********************************')

// filter() method is used to create a new array with all elements that pass the test implemented by the provided function. 返回满足条件的元素组成的新数组

let arr15 = [1, 2, 3, 10, 5];
let filtered = arr15.filter(num => num > 3);
console.log(filtered); // Output: [10, 5]


// fill() method is used to fill the elements of an array from a start index to an end index with a static value.

let arr16 = [1, 2, 3, 4, 5];
arr16.fill(0, 2, 5);
console.log(arr16); // Output: [1, 2, 0, 0, 0]

// every() method is used to test whether all elements in the array pass the test implemented by the provided function. 返回布尔值

let arr17 = [1, 2, 3, 10, 5];
let isEvery = arr17.every(num => num > 3);
console.log(isEvery); // Output: false

// some() method is used to test whether at least one element in the array passes the test implemented by the provided function. 返回布尔值

let arr18 = [1, 2, 3, 10, 5];
let isSome = arr18.some(num => num > 5);
console.log(isSome); // Output: true


// forEach() method is used to execute a provided function once for each element in the array.

let arr19 = [1, 2, 3, 10, 5];
arr19.forEach(num => console.log(num)); // Output: 1 2 3 10 5

// map() method is used to create a new array with the results of calling a provided function on every element in the calling array. 返回新数组

let arr20 = [1, 2, 3, 10, 5];
let newArr20 = arr20.map(num => num * 2);
console.log(newArr20); // Output: [2, 4, 6, 20, 10]

// reduce() method is used to apply a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. 返回单一值

let arr21 = [1, 2, 3, 10, 5];
let sum = arr21.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 21

console.log('*****************************')

// reduceRight() method is used to apply a function against an accumulator and each element in the array (from right to left) to reduce it to a single value. 返回单一值

let arr22 = [1, 2, 3, 10, 5];
let sum2 = arr22.reduceRight((acc, curr) => acc + curr, 0);
console.log(sum2); // Output: 21

